var geneGlobal = {};
geneGlobal.tabOne = 0;
geneGlobal.tabTwo = 0; // 用于tab下拉底部埋点
geneGlobal.minK = 0;
geneGlobal.maxK = 0;
geneGlobal.ajaxFlag = 0;
geneGlobal.tabName = 'score';
geneGlobal.marketid = getParaByName('marketId');
geneGlobal.stockCode = getParaByName("code");
var pageId = 'pdt_pms_269_gene';
var url_ver = {'url_ver':'SJCGBS-14521','targid':'stock_'+geneGlobal.stockCode};
var geneVue;
var scrollObject;
var fluctuation = {
	initAjax:function(flag){
		$.ajax({
			type:'get',
			url:hxmConf.hxmAjaxUrl,
			dataType:'json',
			data:{
				"op" : "getStockGene",
				"stockCode" : geneGlobal.stockCode
			},
			cache:false,
			timeout:10000,
		})
		.fail(function(){
			geneGlobal.ajaxFlag = 0;
			$('.load-box').addClass('hide');
			$('.load-error').removeClass('hide');
		})
		.done(function(data){
			geneGlobal.ajaxFlag = 0;
			if(data.code == 0 && !$.isEmptyObject(data.data)){
				geneGlobal.urlData = data.data;
				handleWantData(data.data);
				if(flag == 'start'){
					fluctuation.setPageVue(); //实例化vue对象
				}else{
					//刷新数据后，重新绘图
					setTimeout(function(){
						geneVue.klineDataMiddle = geneGlobal.urlData.lastDayPrice;
						geneVue.fluctuatData = geneGlobal.urlData;
						geneVue.bestRiseList = geneGlobal.urlData.historyReturn;
						limitUp.initKLineChart(geneGlobal.KLinePrice,geneGlobal.KLineRangePrice,geneGlobal.maxK,geneGlobal.minK);
						(geneGlobal.tabName == 'score') && (limitUp.initRose());
					},2000);
				}
			}else{
				$('.load-box').addClass('hide');
				$('.load-error').removeClass('hide');
			}
		})
	},
	setPageVue:function(){
		geneVue = new Vue({
			el:'.vueBox',
			data:{
				klineDataMiddle : geneGlobal.urlData.lastDayPrice,
				fluctuatData : geneGlobal.urlData,
				clickModelFlag : '0',
				bestRiseList : geneGlobal.urlData.historyReturn
			},
			computed:{
				bestInHandle:function(){  //折线图最佳周期
					var bestKey;
					if(this.bestRiseList.riseData != null && this.bestRiseList.riseData.length != 0){
						var bestRiseDay = Math.max.apply(null,this.bestRiseList.riseData);
						$.each(this.bestRiseList.riseData,function(key,value){
							if(bestRiseDay == value){
								bestKey = key;
							}
						})
						return bestKey;
					}else{
						return '--';
					}
					
				},
				averageRiseUp:function(){  //折线图平均收益
					var sum = 0;
					if(this.bestRiseList.riseData != null && this.bestRiseList.riseData.length != 0){
						$.each(this.bestRiseList.riseData,function(key,value){
							sum += value; 
						});
						return parseFloat(sum/this.bestRiseList.riseData.length).toFixed(2);
					}else{
						return '--';
					}

				},
				chartDataMax:function(){  //折线图纵坐标最大值
					if(this.bestRiseList.riseData != null){
						var  tmax =  Math.max.apply(null,this.bestRiseList.riseData);
						return tmax.toFixed(2);
					}
				},
				chartDataMin:function(){  //折线图纵坐标最小值
					if(this.bestRiseList.riseData != null){
						var tmin = Math.min.apply(null,this.bestRiseList.riseData);
						return tmin.toFixed(2);
					}
				},
				klineDataMax:function(){  //分时图数据处理
					if(typeof geneGlobal.urlData.maxPrice == 'number' && typeof geneGlobal.urlData.minPrice == 'number' && typeof geneGlobal.urlData.lastDayPrice == 'number'){
						var addData;
						var absMax = Math.abs(geneGlobal.urlData.maxPrice - geneGlobal.urlData.lastDayPrice);
						var absMin = Math.abs(geneGlobal.urlData.minPrice - geneGlobal.urlData.lastDayPrice);
						addData =  absMax - absMin > 0 ? geneGlobal.urlData.maxPrice : (parseFloat(geneGlobal.urlData.lastDayPrice) + parseFloat(absMin.toFixed(2)));
						geneGlobal.maxK = addData.toFixed(2);
						return addData.toFixed(2);
					}else{
						return '--';
					}
				},
				klineDataMin:function(){  // k线最大值
					if(typeof geneGlobal.urlData.maxPrice == 'number' && typeof geneGlobal.urlData.minPrice == 'number' && typeof geneGlobal.urlData.lastDayPrice == 'number'){
						var addData;
						var absMax = Math.abs(geneGlobal.urlData.maxPrice - geneGlobal.urlData.lastDayPrice);
						var absMin = Math.abs(geneGlobal.urlData.minPrice - geneGlobal.urlData.lastDayPrice);
						addData =  absMax - absMin > 0 ? (parseFloat(geneGlobal.urlData.lastDayPrice) - parseFloat(absMax.toFixed(2))) : geneGlobal.urlData.minPrice;
						geneGlobal.minK = addData.toFixed(2);
						return addData.toFixed(2);
					}else{
						return '--';
					}
				},
				klineDataRise:function(){  //k线涨幅计算
					if(typeof geneGlobal.urlData.maxPrice == 'number' && typeof geneGlobal.urlData.minPrice == 'number' && typeof geneGlobal.urlData.lastDayPrice == 'number'){
						var addData;
						var absMax = Math.abs(geneGlobal.urlData.maxPrice - geneGlobal.urlData.lastDayPrice);
						var absMin = Math.abs(geneGlobal.urlData.minPrice - geneGlobal.urlData.lastDayPrice);
						addData =  absMax - absMin > 0 ? absMax : absMin;
						return (addData/geneGlobal.urlData.lastDayPrice*100).toFixed(2);
					}else{
						return '--';
					}
				},
				stockAwayData:function(){  //换手基因股性处理
					var overStr = '';
					var overData = 0; 
					if(geneGlobal.urlData.geneData.turnOverRate.data != null){
						overData = geneGlobal.urlData.geneData.turnOverRate.data;
					}else{
						overData = -1;
					}
					if(overData < 3 && overData >= 0){
						overStr = '不是很活跃';
					}else if(overData >=3 && overData <=10){
						overStr = '一般';
					}else if(overData > 10 && overData <=20){
						overStr = '比较活跃';
					}else if(overData == -1){	
						overStr = '--';
					}else{
						overStr = '十分活跃';
					}

					return overStr;
				},
				trendData:function(){  //趋势基因的走势处理
					var scoreStr = '';
					var score = 0;
					if(geneGlobal.urlData.geneData.trend.data != null){
						score = geneGlobal.urlData.geneData.trend.data;
					}else{
						score = -1;
					}
					if(score <=2 && score >= 0){
						scoreStr = '向下';
					}else if(score > 2 && score <= 5){
						scoreStr = '尚不明朗';
					}else if(score > 5 && score <= 7){
						scoreStr = '向上';
					}else if(score == -1){
						scoreStr = '--';
					}else{
						scoreStr = '强劲';
					} 

					return scoreStr;
				},
				compareMoney:function(){  //大单基因盘面梳理
					var comStr = '';
					if(geneGlobal.urlData.geneData.bigMoney.data.bigMoney != null){
						var money = geneGlobal.urlData.geneData.bigMoney.data.bigMoney;
						if(money < 10){
							comStr = '较小';
						}else if(money >=10 && money <=50){
							comStr = '中等';
						}else if(money > 50){
							comStr = '较大';
						}
					}else{
						comStr = '--';
					}
					return comStr;
				},
				shortGoInMoney:function(){  //大单基因的数据显示处理
					var moneyStr = '';
					var str = '';
					if(geneGlobal.urlData.geneData.bigMoney.data.bigMoney != null){
						str = Math.abs(geneGlobal.urlData.geneData.bigMoney.data.bigMoney).toString();
						var length = str.length;
						if(length<9){
							var goinMoney = Math.abs(geneGlobal.urlData.geneData.bigMoney.data.bigMoney/10000);
							moneyStr = goinMoney.toFixed(2) +'万元';
						}else{
							var goinMoney = Math.abs(geneGlobal.urlData.geneData.bigMoney.data.bigMoney/100000000);
							moneyStr = goinMoney.toFixed(2) + '亿元';
						}
						if(geneGlobal.urlData.geneData.bigMoney.data.bigMoney > 0){
							moneyStr = '流出'+moneyStr;
						}else{
							moneyStr = '流入'+moneyStr;
						}
					}else{
						moneyStr = '流出--';
					}
					return moneyStr;
				},
				changeJiYin:function(){
					if(this.fluctuatData.geneData.turnOverRate.data != null){
						return this.fluctuatData.geneData.turnOverRate.data.toFixed(2);
					}else{
						return '--';
					}
				},
				geguMoney:function(){
					var monStr = '';
					var monData = 0;
					if(geneGlobal.urlData.geneData.bigMoney.data.circulationMarketValue != null){
						monData = parseInt(geneGlobal.urlData.geneData.bigMoney.data.circulationMarketValue);
					}else{
						monData = 0;
					}
					var dataStr = monData.toString();
					if(dataStr.length < 9){
						monStr = monData/10000;
						if(monStr == 0){
							monStr = '--';
						}else{
							monStr = monStr.toFixed(2) +'万元';
						}
					}else{
						monStr = monData/100000000;
						monStr = monStr.toFixed(2) + '亿元';
					}
					return monStr;
				},
				startDate:function(){  //柱状图开始日期
					var arrDate = [];
					$.each(geneGlobal.urlData.historyReturn.limitUpTimesByMonth,function(key,value){
						arrDate.push(key);
					});
					return arrDate[0];
				},
				endDate:function(){   //柱状图结束日期
					var arrDate = [];
					$.each(geneGlobal.urlData.historyReturn.limitUpTimesByMonth,function(key,value){
						arrDate.push(key);
					});
					return arrDate[arrDate.length-1];
				},
				averageRise:function(){
					if(this.fluctuatData.geneData.shortActive.data.averageAmplitude10 != null){
						return this.fluctuatData.geneData.shortActive.data.averageAmplitude10.toFixed(2);
					}else{
						return '--';
					}
					
				},
				averageRiseTwo:function(){  //短期活跃基因
					if(this.fluctuatData.geneData.shortActive.data.limitUp30 != null){
						return this.fluctuatData.geneData.shortActive.data.limitUp30;
					}else{
						return '--';
					}
				},
				averageRiseThree:function(){
					if(this.fluctuatData.geneData.shortActive.data.highestRise30 != null){
						return this.fluctuatData.geneData.shortActive.data.highestRise30;
					}else{
						return '--';
					}
				},
				averageRiseFour:function(){
					if(this.fluctuatData.geneData.shortActive.data.limitUp10 != null){
						return this.fluctuatData.geneData.shortActive.data.limitUp10;
					}else{
						return '--';
					}
				},
				averageRiseFive:function(){
					if(this.fluctuatData.geneData.shortActive.data.highestRise10 != null){
						return this.fluctuatData.geneData.shortActive.data.highestRise10;
					}else{
						return '--';
					}
				},
				riseInOpen:function(){
					if(this.fluctuatData.geneData.timeSharing.data.riseInOpen != null){
						return this.fluctuatData.geneData.timeSharing.data.riseInOpen.toFixed(2);
					}else{
						return '--';
					}
				},
				riseToday:function(){
					if(this.fluctuatData.geneData.timeSharing.data.highestRiseToday != null){
						return this.fluctuatData.geneData.timeSharing.data.highestRiseToday.toFixed(2);
					}else{
						return '--';
					}
				},
				lowestRiseToday:function(){
					if(this.fluctuatData.geneData.timeSharing.data.lowestRiseToday != null){
						return this.fluctuatData.geneData.timeSharing.data.lowestRiseToday.toFixed(2);
					}else{
						return '--';
					}
				},
				riseInClose:function(){
					if(this.fluctuatData.geneData.timeSharing.data.riseInClose != null){
						return this.fluctuatData.geneData.timeSharing.data.riseInClose.toFixed(2);
					}else{
						return '--';
					}
				},
				conMore:function(){
					if(this.fluctuatData.geneData.popularity.data != null){
						return this.fluctuatData.geneData.popularity.data;
					}else{
						return '--';
					}
				},
				breakMore:function(){
					if(this.fluctuatData.geneData.breakthrough.data != null){
						return this.fluctuatData.geneData.breakthrough.data;
					}else{
						return '--';
					}
				},
				shinkMore:function(){
					if(this.fluctuatData.geneData.shrinkageUp.data != null){
						return this.fluctuatData.geneData.shrinkageUp.data;
					}else{
						return '--';
					}
				},
				sealRateData:function(){
					if(this.fluctuatData.historyReturn.sealRate != null){
						return this.fluctuatData.historyReturn.sealRate;
					}else{
						return '--';
					}
				},
				nextAverageRise:function(){ //最佳持股周期
					if(this.bestRiseList.riseData.length != 0){
						return this.bestRiseList.riseData[1];
					}else{
						return '--';
					}
				},
				priceChange:function(){  //股票涨幅
					if( typeof this.fluctuatData.change == 'number'){
						return this.fluctuatData.change.toFixed(2);
					}else{
						return '--';
					}
				}
			},
			methods:{
				switchTab:function(flag){   //涨停基因和历史回看切换
					$('.ztDetail-box div span').hide();
					$('.ztDetail-box div').removeClass('redcolor');
					if(flag == '1'){
						//上市满30个交易日
						this.clickModelFlag = '1';
						geneGlobal.tabName = 'his';
						hxmClickStat(pageId+'.his',url_ver);
						$('.ztDetail-box div:nth-child(2) span').show();
						$('.ztDetail-box div:nth-child(2)').addClass('redcolor');
						if(this.fluctuatData.historyReturn.riseData != null && this.fluctuatData.historyReturn.limitUpTimesByMonth != null){
							if(this.fluctuatData.historyReturn.riseData.length != 0 && this.fluctuatData.historyReturn.limitUpTimesByMonth.length != 0){
								setTimeout(function(){  //画柱状图和折线图
									var data1=[],data2=[],data3=[];
									$.each(geneGlobal.urlData.historyReturn.limitUpTimesByMonth,function(key,value){
										data1.push(value.sealTimes);
										data2.push(value.touchTimes);
										data3.push(key);
									});
									limitUp.initGraph(data1,data2,data3);
			    					limitUp.initLine(geneGlobal.urlData.historyReturn.riseData);
			    					var boxWidth = $('#chartBox').width();
			    					$('.chartline').css('width',boxWidth+'px');
								},300);
							}else{
								setTimeout(function(){
									$('.graph_dash').hide();
									$('.graph_box').css('height','1.9rem');
									$('.graph_mask,.show_box').text('该股近一年都无涨停，暂无历史回看数据');
									$('.graph_mask,.show_box').css({
										'line-height':'1.9rem',
										'text-align':'center',
										'color':'#323232',
										'font-size':'0.32rem',
										'display':'block',
										'border':'none !important'
									});
								},500);
							}
						}else{  //不满30个交易日的股票处理
							setTimeout(function(){
								$('.graph_dash').hide();
								$('.graph_box').css('height','1.9rem');
								$('.graph_mask,.show_box').text('该股为新上市股票，暂无历史回看数据');
								$('.graph_mask,.show_box').css({
									'line-height':'1.9rem',
									'text-align':'center',
									'color':'#323232',
									'font-size':'0.32rem',
									'display':'block',
									'border':'none !important'
								});
							},500);
							
						}
					}else{
						this.clickModelFlag = '0';
						geneGlobal.tabName = 'score';
						hxmClickStat(pageId+'.score',url_ver);
						$('.ztDetail-box div:nth-child(1) span').show();
						$('.ztDetail-box div:nth-child(1)').addClass('redcolor');
						setTimeout(function(){ //画玫瑰图
							limitUp.initRose();
						},300);

						var tt = setInterval(function(){
							if($('.rose-score').height() > 1){
								$('.rose-score').css('height','');
							}
						},5);
						
					}
				},
				moreInformationModel:function(){  //涨停基因功能说明弹窗打开
					hxmClickStat(pageId+'.intro',url_ver);
					$('.web_filter').show();
					$('.filter_box').show();
					scrollObject.downPullEnable = false;  //禁止页面下拉
					var getboxH = setInterval(function(){
						var height = $('.filter_box').height();
						if(height > 1){
							clearInterval(getboxH);
							var scrollBox = new IScroll('.filter_contain');
							scrollBox.refresh();
						}
					},20);
					$('.web_filter,body').on('touchmove',function(e){
						e.preventDefault();
					})
				},
				closeModelInformation:function(){  //涨停基因功能说明弹窗关闭
					hxmClickStat(pageId+'.intro.ok',url_ver);
					$('.web_filter').hide();
					$('.filter_box').hide();
					scrollObject.downPullEnable = true;  //关闭遮罩后，打开下拉锁
					$('.web_filter,body').off('touchmove');
				},
				JumpToFenShi:function(code,marketid){  //点击分时图，跳转客户端分时
					if(!code || !marketid){
						return false;
					}
					hxmJumpNativeStat(pageId+'.fenshi','2205',url_ver);
					window.location.href = 'client.html?action=ymtz^stockcode='+code+'^marketid='+marketid+'^webid=2205';
				},
				transFormData:function(str,type){
					if(typeof str == type){
						return str.toFixed(2);
					}else{
						return '--';
					}
				}
			}
		});

		Vue.nextTick(function(){
			setTimeout(function(){
				limitUp.initKLineChart(geneGlobal.KLinePrice,geneGlobal.KLineRangePrice,geneGlobal.maxK,geneGlobal.minK);
				limitUp.initRose();
			},300);

			// 下拉刷新	
			setTimeout(function(){
				$('.load-box').addClass('hide');
				var dpr = $('html').data('dpr');
			    scrollObject = $('.scroll_box').pullScroll({
			        el:".vueBox",
			        downPullEnable:true,
			        upPullEnable:false,
			        scrollJudgeSize:150*dpr,
			        downPullEvent:function(){
			      		if(geneGlobal.ajaxFlag == 0){
			      			geneGlobal.ajaxFlag = 1;
			      			fluctuation.initAjax("refresh");
			      			hxmClickStat(pageId+'.refresh',url_ver);
			      		}
			        }
			    });
			},500);

			//注册前后台事件，处理分时返回页面，折线图画不出来
			try{
				registerWebListener("onShow", function(){
					switch(geneGlobal.tabName){
						case 'score':
							limitUp.initKLineChart(geneGlobal.KLinePrice,geneGlobal.KLineRangePrice,geneGlobal.maxK,geneGlobal.minK);
							setTimeout(function(){
								limitUp.initRose();
							},300);
							break;
						case 'his':
							limitUp.initKLineChart(geneGlobal.KLinePrice,geneGlobal.KLineRangePrice,geneGlobal.maxK,geneGlobal.minK);
							if(this.fluctuatData.historyReturn.riseData != null && this.fluctuatData.historyReturn.limitUpTimesByMonth != null){
								if(this.fluctuatData.historyReturn.riseData.length != 0 && this.fluctuatData.historyReturn.limitUpTimesByMonth.length != 0){
									setTimeout(function(){  //画柱状图和折线图
										var data1=[],data2=[],data3=[];
										$.each(geneGlobal.urlData.historyReturn.limitUpTimesByMonth,function(key,value){
											data1.push(value.sealTimes);
											data2.push(value.touchTimes);
											data3.push(key);
										});
										limitUp.initGraph(data1,data2,data3);
				    					limitUp.initLine(geneGlobal.urlData.historyReturn.riseData);
									},300);
								}
							}
							break;	
					}
		        });

		        // 切换到后台时
		        registerWebListener("onHide", function(){});
			}catch(e){
				console.log(e);
			}

		});
	}
};


//处理页面左上角分时图数据
function handleWantData(data){
	geneGlobal.KLinePrice = [];
	geneGlobal.KLineRangePrice = [];
	$.each(data.timeSharing,function(key,value){
		geneGlobal.KLinePrice.push(value.price);
		geneGlobal.KLineRangePrice.push(value.rangePrice);
	});

}



// 滑动到底部埋点
function bottomStat(){
    $(window).on('scroll', function(){
        var dpr = $('html').data('dpr') ? $('html').data('dpr') : 1; // 页面dpr
        var windowHeight = getPlatform() == 'iphone' ? window.screen.height : $(window).height();//分辨率的高度
        var scrollHeight = $(window).scrollTop() / dpr;//document.documentElement.scrollTop;//滚动的高度
        var totalHeight = $(document).height() / dpr;//文档的总高度
        
        if((scrollHeight) >= (totalHeight - windowHeight)){
            if(geneGlobal.tabName == 'score'){
            	if(geneGlobal.tabOne == 0){
            		geneGlobal.tabOne = 1;
            		hxmClickStat(pageId+'.score.bottom',url_ver);
            	}
            }else{
            	if(geneGlobal.tabTwo == 0){
            		geneGlobal.tabTwo = 1;
            		hxmClickStat(pageId+'.his.bottom',url_ver);
            	}
            }
        }
    });
}



;(function(){
	$('.load-box').removeClass('hide');
	$('.ztDetail-box div span').hide();
	$('.ztDetail-box div:nth-child(1) span').show();
	$('.ztDetail-box div:nth-child(1)').addClass('redcolor');
	fluctuation.initAjax('start');
	//页面打开埋点
	hxmPageStat({'id':pageId,'url_ver':'SJCGBS-14521','targid':'stock_'+geneGlobal.stockCode});
	//滚动到底部埋点
	bottomStat();

	//接口异常，点击再次请求
	$('.cover').on('click',function(){
		$('.load-error').addClass('hide');
		$('.load-box').removeClass('hide');
		fluctuation.initAjax('start');
	})

})();


